package cn.itcast.web.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * SpringMVC全局异常：自动捕获到控制器方法的异常
 */
@Component // 创建对象，加入容器
public class CustomException implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(
            HttpServletRequest request, HttpServletResponse response,
            Object handler, Exception ex) {
        // 控制器打印异常
        ex.printStackTrace();
        // 返回
        ModelAndView mv = new ModelAndView("error");
        return mv;
    }
}
